package com.shiwaixiangcun.customer.module.property

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.google.gson.reflect.TypeToken
import com.lzy.okgo.model.HttpParams
import com.shiwaixiangcun.customer.GlobalApi
import com.shiwaixiangcun.customer.R
import com.shiwaixiangcun.customer.adapter.AdapterFeeItem
import com.shiwaixiangcun.customer.base.BaseActivity
import com.shiwaixiangcun.customer.entity.CommonData
import com.shiwaixiangcun.customer.entity.FeeItem
import com.shiwaixiangcun.customer.entity.RecordDetailEntity
import com.shiwaixiangcun.customer.network.NetworkHelper
import com.shiwaixiangcun.customer.utils.DateUtil
import com.shiwaixiangcun.customer.utils.JsonUtil
import kotlinx.android.synthetic.main.activity_record_detail.*
import kotlinx.android.synthetic.main.top_bar_blue.*

/**
 * 缴费记录详情
 */
class RecordDetailActivity : BaseActivity(), View.OnClickListener {
    private var mAdapter: AdapterFeeItem? = null

    private var mList: MutableList<FeeItem>? = null
    private var houseID: Long? = null
    private var mNetworkHelper: NetworkHelper? = null
    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.back_left -> {
                finish()
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_record_detail)
        initBundleData()
        initViewAndEvent()
        initData()
    }

    private fun initData() {
        val httpParams = HttpParams()

        houseID?.let { httpParams.put("statementId", it) }
        mNetworkHelper?.getRequestWithToken(GlobalApi.recordsDetail, true, httpParams, object : NetworkHelper.NetWorkHelperBack {
            override fun requestSuccess(json: String) {
                val type = object : TypeToken<CommonData<RecordDetailEntity>>() {}.type
                val fromJson = JsonUtil.fromJson<CommonData<RecordDetailEntity>>(json, type)
                        ?: return

                if (fromJson.responseCode == 1001) {
                    tv_date.text = fromJson.data.detail[0].name
                    tv_fee.text = "￥${fromJson.data.amount}"
                    tv_fee_total.text = "￥${fromJson.data.amount}"
                    tv_payment_propety.text = fromJson.data.house
                    tv_payment_company.text = fromJson.data.property
                    tv_payment_payway.text = fromJson.data.payWay
                    tv_payment_date.text = DateUtil.getTimeFormatString(fromJson.data.payDate)
                    mList?.addAll(fromJson.data.detail[0].list)
                    mAdapter?.notifyDataSetChanged()


                }


            }

            override fun requestFail() {
            }
        })

    }

    private fun initBundleData() {

        houseID = intent.extras.getLong("houseId")
    }

    /**
     * 初始化
     */
    private fun initViewAndEvent() {
        tv_page_name.text = "账单详情"
        back_left.setOnClickListener(this)
        mNetworkHelper = NetworkHelper(mContext, supportFragmentManager)
        mList = ArrayList()
        mAdapter = AdapterFeeItem(mList)
        rv_attr.layoutManager = LinearLayoutManager(mContext)
        rv_attr.adapter = mAdapter

    }
}
